package com.hackathon.chgk;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Properties;

import com.hackathon.chgk.taskloaders.TaskLoader;
import com.hackathon.chgk.taskloaders.impl.TaskLoaderImpl;
import com.hackathon.chgk.utils.NetworkMode;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.util.Log;

public class ChgkApplication {
	
	private Calendar calendar;	
	private Properties properties;
	private Context currentContext;
	private TaskLoader taskLoader;
	private static ChgkApplication instance = null;
	private NetworkMode networkMode;
	
	
	static final String CLASSNAME = ChgkApplication.class.getName();
	static final String PROPERTIES_FILENAME = "common.properties";
	static final String PROPERTIES_LOAD_EXCEPTION = "Failed to open property file";
	static final String PROPERTIES_ONLOAD_MESSAGE = "The properties are now loaded";
	
	private ChgkApplication () {
		
		super();
		setCalendar(Calendar.getInstance());		
	}
	
	public static ChgkApplication getInstance() {
		if(instance == null) {
			instance = new ChgkApplication();
		}
		return instance;		
	}		

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}
	
	public boolean isOnline() {
		
		ConnectivityManager cm =
	        (ConnectivityManager) currentContext.getSystemService(Context.CONNECTIVITY_SERVICE);

	    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
	}

	public Context getCurrentContext() {
		return currentContext;
	}

	public void setCurrentContext(Context currentContext) {
		this.currentContext = currentContext;
	}
	
	public TaskLoader getTaskLoader() {		
		
		SharedPreferences prefSettings = getCurrentContext().getApplicationContext()
				.getSharedPreferences("mode_list_pref", Context.MODE_PRIVATE);	
		NetworkMode nm = NetworkMode.valueOf(
				prefSettings.getString("mode_list_pref", "ONLINE"));
		
		if ((taskLoader == null) || (networkMode != nm)) {
			
			networkMode = nm;
			
			Log.d("Networkmode", networkMode.toString());
			
			if (networkMode == NetworkMode.ONLINE) {
				if (!isOnline())
					networkMode = NetworkMode.MIXED;
			}
			taskLoader = new TaskLoaderImpl(
					this.getProperties().getProperty("db_url"),
					networkMode,
					this.currentContext);
		}			
		return taskLoader;		
	}

	public void setTaskLoader(TaskLoader taskLoader) {
		this.taskLoader = taskLoader;
	}
	
	private Properties getProperties() {
		if (properties == null) {
			properties = new Properties();
			AssetManager assetManager = currentContext.getAssets();
	
			try {
	
				InputStream inputStream = assetManager
						.open(PROPERTIES_FILENAME);
				properties.load(inputStream);
				Log.d(CLASSNAME, PROPERTIES_LOAD_EXCEPTION);
				System.out.println(PROPERTIES_ONLOAD_MESSAGE);
				System.out.println("properties: " + properties);
			} catch (IOException e) {
	
				Log.e(TaskLoaderImpl.class.toString(), PROPERTIES_LOAD_EXCEPTION);
				e.printStackTrace();
			}	
		}
		return properties;
	}
	
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

}
